package DAL;

import lejos.pc.comm.NXTComm;
import lejos.pc.comm.NXTCommBluecove;
import lejos.pc.comm.NXTCommException;
import lejos.pc.comm.NXTCommFactory;
import lejos.pc.comm.NXTInfo;

/**
 * Creates a NXTComm object and manages searching for discoverable
 * bluetooth NXT devices.
 * Uses the singleton pattern to make sure only one instance is created.
 * 
 * @author Danny Johansen, Didzis Petkus, Liviu Berheci.
 *
 */
public class BTCom {

	private static BTCom instance;
	private NXTComm con;

	/**
	 * Private constructor for maintaining the singleton pattern.
	 * @throws NXTCommException if bluetooth error occurs.
	 */
	private BTCom() throws NXTCommException {
		con = NXTCommFactory.createNXTComm(NXTCommFactory.BLUETOOTH);
	}

	/**
	 * Returns the instance of BTCom.
	 * @return - the BTCom object or a new instance if none exists.
	 * @throws NXTCommException if bluetooth error occurs.
	 */
	public static BTCom getInstance() throws NXTCommException {
		return instance = instance == null ? new BTCom() : instance;
	}

	/**
	 * Initiates a bluetooth search for any NXT bluetooth devices.
	 * @return NXTInfo array of found NXT devices detected.
	 * @throws NXTCommException if bluetooth error occurs.
	 */
	public NXTInfo[] searchNXTs() throws NXTCommException {
		con.search(null);
		return new NXTCommBluecove().search(null);
	}
}
